﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.BLL;
using System.Text;
using DP.Web.UI;
using CRM.Entity;
using DP.Common;

namespace CRM.Web.MainManage
{
    public partial class ToDoOutbound : Common.PageBase
    {
        public string VoiceMailModifyUrl = "~/OutboundManage/VoiceModify.aspx?ID={0}";
        public string LostCallModifyUrl = "~/OutboundManage/LostModify.aspx?ID={0}";

        private T_VOICEMAIL_BLL voiceMailBll = new T_VOICEMAIL_BLL();
        T_CALLLOSS_BLL callLossBll = new T_CALLLOSS_BLL();

        /// <summary>
        /// 
        /// </summary>
        private T_CONTENTAUTHORITY_BLL caBll = new T_CONTENTAUTHORITY_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_ROLEORDERSTATUSAUTHORITY_BLL roleOrderStatusAuthorityBll = new T_ROLEORDERSTATUSAUTHORITY_BLL();

     
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvDataBinding();
            }
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationVoiceMailSQLString()
        {
            StringBuilder sql = new StringBuilder();
            

            #region 权限部分
            if (!UserLoginInfo.IsAdministrator())
            {
                //操作员当前事业务数据
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");

                //操作员有权看到人员的数据
                sql.Append(caBll.GenerateContentAuthorityConditions(_userLoginInfo, "AssignedPersonID"));

                //
                sql.Append(String.Format(" and STATUS in ({0}) ", ((int)VoiceStatus.待回电).ToString() + "," + ((int)VoiceStatus.未接通).ToString()));
            }
            #endregion

            return sql;

        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationLostCallSQLString()
        {
            StringBuilder sql = new StringBuilder();

            if (!UserLoginInfo.IsAdministrator())
            {
                //操作员当前事业务数据
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");

                //操作员有权看到人员的数据
                sql.Append(caBll.GenerateContentAuthorityConditions(_userLoginInfo, "AssignedPersonID"));

                //
                sql.Append(String.Format(" and STATUS in ({0}) ", ((int)LostStatus.待回电).ToString() + "," + ((int)LostStatus.未接通).ToString()));

            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sqlVoiceMail = GenerationVoiceMailSQLString();
            StringBuilder sqlLostCall = GenerationLostCallSQLString();
            #endregion

            List<T_VOICEMAIL_Entity> voiceMailList;
            List<T_CALLLOSS_Entity> callLossList;

            //留言
            gvListVoiceMail.RecordCount = (int)voiceMailBll.GetCount(sqlVoiceMail.ToString());
            voiceMailList = voiceMailBll.GetList(sqlVoiceMail.ToString(), gvListVoiceMail.CurrentPageIndex * gvListVoiceMail.PageSize, gvListVoiceMail.PageSize, gvListVoiceMail.SortConditions);
            gvListVoiceMail.DataSource = voiceMailList;
            gvListVoiceMail.DataKeyNames = new string[] { "ID", "AllocatedTime", "limited" };
            gvListVoiceMail.DataBind();
            gvListVoiceMail.Caption = "<a href='../OutboundManage/ToDoVoiceList.aspx' target='frameMain' >留言 ( " + gvListVoiceMail.RecordCount + " ) </a>";


            //呼损
            gvListLostCall.RecordCount = (int)callLossBll.GetCount(sqlLostCall.ToString());
            callLossList = callLossBll.GetList(sqlLostCall.ToString(), gvListLostCall.CurrentPageIndex * gvListLostCall.PageSize, gvListLostCall.PageSize, gvListLostCall.SortConditions);
            gvListLostCall.DataSource = callLossList;
            gvListLostCall.DataKeyNames = new string[] { "ID", "AllocatedTime", "limited" };
            gvListLostCall.DataBind();
            gvListLostCall.Caption = "<a href='../OutboundManage/ToDoLostList.aspx' target='frameMain' >呼损 ( " + gvListLostCall.RecordCount + " ) </a>";


        }
              
        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {


        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    case "处理状态":
                                        {
                                            try
                                            {
                                                HyperLink hl = e.Row.Cells[i].FindControl("hlModify3") as HyperLink;
                                                temp = hl.Text;
                                                switch(gv.ID)
                                                {
                                                    case "gvListVoiceMail":
                                                        {
                                                            try
                                                            {
                                                                temp = ((VoiceStatus)Enum.Parse(typeof (VoiceStatus), temp)).ToString();
                                                            }
                                                            catch
                                                            {
                                                                
                                                            }
                                                        }
                                                        break;
                                                    case "gvListLostCall":
                                                        {
                                                            try
                                                            {
                                                                temp = ((LostStatus)Enum.Parse(typeof(LostStatus), temp)).ToString();
                                                            }
                                                            catch
                                                            {

                                                            }
                                                        }
                                                        break;
                                                }
                                                hl.Text = temp;

                                            }
                                            catch (Exception)
                                            {

                                            }

                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }

                        #region 超期样式
                        DateTime dt = DateTime.Now;
                        temp = (gv.DataKeys[e.Row.RowIndex].Values["AllocatedTime"] == null) ? "" : gv.DataKeys[e.Row.RowIndex].Values["AllocatedTime"].ToString();
                        if (DateTime.TryParse(temp, out dt))
                        {
                            temp = (gv.DataKeys[e.Row.RowIndex].Values["limited"] == null)
                                       ? ""
                                       : gv.DataKeys[e.Row.RowIndex].Values["limited"].ToString();
                            if (StringHelper.IsNumber(temp))
                            {
                                if (dt.AddHours(StringHelper.ToInt(temp)).CompareTo(DateTime.Now) < 0)
                                {
                                    try
                                    {
                                        (e.Row.FindControl("hlModify1") as HyperLink).Attributes["style"] =
                                            configBll.GetCacheValueByName(this.Page, "ExtendedStyle");
                                        (e.Row.FindControl("hlModify2") as HyperLink).Attributes["style"] =
                                            configBll.GetCacheValueByName(this.Page, "ExtendedStyle");
                                        (e.Row.FindControl("hlModify3") as HyperLink).Attributes["style"] =
                                            configBll.GetCacheValueByName(this.Page, "ExtendedStyle");
                                    }
                                    catch (Exception)
                                    {

                                    }
                                }
                            }
                        }

                        #endregion
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion


    }
}
